<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
	<%@include file="../include.jsp"%>
	<script	src="../static/lib/ztree/jquery.ztree.all-3.5.min.js"></script>
	<link  href="../static/lib/ztree/zTreeStyle.css" type="text/css" rel="stylesheet" >
</head>
<body id="z_body">
	<div class="panel">
		<div class="panel-heading">
			<i class="icon icon-list"></i> 
			组织结构使用维护
		</div>
		<div class="panel-body">
			<div class="row">
				<div class="col-md-8" style="width: 500px;">
					<form class="form-horizontal" id="form1">
					<input name="zyids"  id="zyids" value="" type="hidden"/>
					
					
						<div class="form-group">
								<label class="col-md-3 app_required">选择应用</label>
								<div class="col-md-6">
									<select class="form-control" name="zdbh" id="zdbh" onchange="showTree()"></select>
								</div>
							</div>
					</form>
				</div>
				<div class="col-md-4">
						<div>
							<span style="padding-right: 2em;">选择需要使用的组织结构</span>
							<label style="font-weight:normal;"><input type="checkbox" onclick="checkAll(this)" id="checkAll"/> 全选/清空</label>
						</div>
						<div id="tree_1" class="ztree"></div>
					</div>
			</div>
		
			<div style="text-align: center;">
				<a class="btn btn-primary" onclick="save()">保存</a>
			</div>
			
		</div>
	</div>
	
	
<script type="text/javascript">
	var treeSetting = {
		check: {
			enable: true,
			chkStyle: "checkbox",
			chkboxType: { "Y": "ps", "N": "ps" }
		},
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "dwdm",
				pIdKey: "gxdwdm"
			},
			key:{name:'dwmc'}
		},
		callback: {
			beforeClick: function(e, treeId, treeNode) {
			},
			onClick: function(e, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj(treeId);
				zTree.checkNode(treeNode,null,true);
			},
			onCheck: function(e, treeId, treeNode){
			}
		}
	};
	var treeNodes;
	
	$(function(){
		getYy();
	});
	
	function getYy(){
		tool.query("../zzjgsy/getYy", {}, function(res) {
			var data = res.data;
			$("#zdbh").html(data);
		}, {});
	}
	
	function showTree(){
		var yy = $("#zdbh").val();
		$("#zyids").val("");
		$("#checkAll").prop("checked",false);
		if("-1" == yy){
			$("#tree_1").html("");
		}else{
			loadTree();
		}
	}
	
	function loadTree(){
		var zdbh = $("#zdbh").val();
		tool.query("../zzjgsy/loadAllZzRes", {zdbh: zdbh}, function(res) {
			var data = res.data;
			$("#zyids").val(data.ids);
			treeNodes = data.allData;
			if(treeNodes != null) {
				initTree();
			}
		}, {});
	}
	
	function initTree() {
		//加载tree
		$.fn.zTree.init($("#tree_1"), treeSetting, treeNodes);
		$.fn.zTree.getZTreeObj("tree_1").expandAll(false);
		//自动勾选节点
		var zyids = $("#form1 [name='zyids']").val();
		if(zyids.length > 0) {
			var tree = $.fn.zTree.getZTreeObj("tree_1");
			$(zyids.split(",")).each(function(){
				var node = tree.getNodeByParam("dwdm",this);
				if(node != null){
					tree.checkNode(node, true);
				}
			});
		}
	}

	/**
	 * 保存
	 */
	function save() {
		//赋值到表单:勾选的资源节点ids
		if(treeNodes != null) {
			$("#form1 [name='zyids']").val(getCheckedResIds());
			//提交
			tool.save("save", $("#form1").serialize(), function(res) {});
		}
	}
	
	//获取勾选ids
	function getCheckedResIds() {
		var v = "";
		var zTree = $.fn.zTree.getZTreeObj("tree_1");
		var nodes = zTree.getCheckedNodes();
		nodes.sort(function compare(a,b){return a.id-b.id;});
		$(nodes).each(function(){
			if(v.length > 0) {
				v += ",";
			}
			v += this.dwdm;
		});
		return v;
	}
	
	//全选
	function checkAll(dom) {
		var zTree = $.fn.zTree.getZTreeObj("tree_1");
		if($(dom).is(":checked")) {
			zTree.checkAllNodes(true);
		} else {
			zTree.checkAllNodes(false);
		}
	}
</script>

</body>
</html>